package com.example.vae;

import android.app.Application;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.vae.Dao.PersonData.PersonReposity;
import com.example.vae.Pojo.Person;

import java.util.List;
import java.util.logging.LogRecord;

public class MySettingViewModel extends AndroidViewModel {
    // TODO: Implement the ViewModel

    private PersonReposity personReposity;
    private MutableLiveData<Person> personLiveData;
    private MySettingListener mySettingListener;
    private static MyHandle myHandle;

    public void setMySettingListener(MySettingListener mySettingListener) {
        this.mySettingListener = mySettingListener;
    }

    //    定义一个消息，发送给外部
    class   MyHandle extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            mySettingListener.success((Person)msg.obj);
        }
    }

    public MySettingViewModel(@NonNull final Application application) {
        super(application);
        Log.d("MySettingViewModel","ing........");
//        初始化类
        myHandle=new MyHandle();
        personReposity = new PersonReposity(application);
        personReposity.findAllPerson();
        personLiveData = new MutableLiveData<>();
        personLiveData = personReposity.getPersonLiveData();
        personReposity.setPersonListener(new PersonReposity.PersonListener() {
            @Override
            public void success(List<Person> people) {
                Log.d("MySettingViewModel", "进行了");
                personLiveData.setValue(people.get(0));
                Message message=new Message();
                message.obj=people.get(0);
                myHandle.sendMessage(message);
            }
        });
    }

    public interface MySettingListener{
        void success(Person person);
    }
    public LiveData<Person> getLiveData() {
        return personLiveData;
    }


}
